package com.gridmobi.xrtb.job;

import com.alibaba.fastjson.JSONObject;
import com.gridmobi.xrtb.domain.dsp.DspTrackLog;
import com.gridmobi.xrtb.handler.BiddingLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author huangyongchao
 */
//@Component
public class ErrorLogHandler {

    @Autowired
    @Qualifier("kafkaBiddingLogger")
    private BiddingLogger biddingLogger;

    private static final Logger logger = LoggerFactory.getLogger(ErrorLogHandler.class);
    @PostConstruct
    public void  init(){
        collectLog();
    }

   // @Scheduled(fixedDelay = 120*1000)
    public void collectLog() {

        String winlog1 = "/opt/xrtb/log/win/win20190330.log";
        String winlog = "/opt/xrtb/log/win/win20190330.log";
        String implog1 = "/opt/xrtb/log/imp/imp20190330.log";
        String implog = "/opt/xrtb/log/imp/imp.log";
        String clicklog1 = "/opt/xrtb/log/click/click20190330.log";
        String clicklog = "/opt/xrtb/log/click/click.log";


        BufferedReader bfr = null;
        try {
            Path fpath = Paths.get(winlog1);
            if (Files.exists(fpath)) {
                bfr = Files.newBufferedReader(fpath);

                bfr.lines().forEach(n -> {
                    try {
                        int st = n.indexOf("{");
                        n = n.substring(st, n.length());
                        biddingLogger.winlog(JSONObject.parseObject(n, DspTrackLog.class));
                    } catch (Exception e) {
                        logger.error("error:", e);

                    }
                });
            }

        } catch (Exception e) {
            logger.error("Read File Error!!!!", e);


        } finally {
            if (bfr != null) {
                try {
                    bfr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }


        try {
            Path fpath = Paths.get(winlog);
            if (Files.exists(fpath)) {
                bfr = Files.newBufferedReader(fpath);

                bfr.lines().forEach(n -> {
                    try {
                        int st = n.indexOf("{");
                        n = n.substring(st, n.length());
                        biddingLogger.winlog(JSONObject.parseObject(n, DspTrackLog.class));
                    } catch (Exception e) {
                        logger.error("error:", e);

                    }
                });
            }

        } catch (Exception e) {
            logger.error("Read File Error!!!!", e);


        } finally {
            if (bfr != null) {
                try {
                    bfr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

        /*try {
            Path fpath = Paths.get(implog1);
            if (Files.exists(fpath)) {
                bfr = Files.newBufferedReader(fpath);

                bfr.lines().forEach(n -> {
                    try {
                        int st = n.indexOf("{");
                        n = n.substring(st, n.length());
                        biddingLogger.imprlog(JSONObject.parseObject(n, DspTrackLog.class));
                    } catch (Exception e) {
                        logger.error("error:", e);

                    }
                });
            }

        } catch (Exception e) {
            logger.error("Read File Error!!!!", e);


        } finally {
            if (bfr != null) {
                try {
                    bfr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

        try {
            Path fpath = Paths.get(implog);
            if (Files.exists(fpath)) {
                bfr = Files.newBufferedReader(fpath);

                bfr.lines().forEach(n -> {
                    try {
                        int st = n.indexOf("{");
                        n = n.substring(st, n.length());
                        biddingLogger.imprlog(JSONObject.parseObject(n, DspTrackLog.class));
                    } catch (Exception e) {
                        logger.error("error:", e);

                    }
                });
            }

        } catch (Exception e) {
            logger.error("Read File Error!!!!", e);


        } finally {
            if (bfr != null) {
                try {
                    bfr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

        try {
            Path fpath = Paths.get(clicklog1);
            if (Files.exists(fpath)) {
                bfr = Files.newBufferedReader(fpath);

                bfr.lines().forEach(n -> {
                    try {
                        int st = n.indexOf("{");
                        n = n.substring(st, n.length());
                        biddingLogger.clicklog(JSONObject.parseObject(n, DspTrackLog.class));
                    } catch (Exception e) {
                        logger.error("error:", e);

                    }
                });
            }

        } catch (Exception e) {
            logger.error("Read File Error!!!!", e);


        } finally {
            if (bfr != null) {
                try {
                    bfr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

        try {
            Path fpath = Paths.get(clicklog);
            if (Files.exists(fpath)) {
                bfr = Files.newBufferedReader(fpath);

                bfr.lines().forEach(n -> {
                    try {
                        int st = n.indexOf("{");
                        n = n.substring(st, n.length());
                        biddingLogger.clicklog(JSONObject.parseObject(n, DspTrackLog.class));
                    } catch (Exception e) {
                        logger.error("error:", e);

                    }
                });
            }

        } catch (Exception e) {
            logger.error("Read File Error!!!!", e);


        } finally {
            if (bfr != null) {
                try {
                    bfr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }*/

    }

    public static void main(String[] args) {


        String s = "2019-03-31 04:43:52,385 [ WARN] http-apr-20080-exec-46 (winlog.:) - {\"channel\":\"smaato\",\"bidid\":\"dd4b2a4d-1503-43ce-ad80-ccadf8e821851\",\"geo\":\"AUS\",\"cid\":\"63\",\"devid\":\"CEB26F1A-5275-4E65-99EA-194C9E3D23BC\",\"reqid\":\"dd4b2a4d-1503-43ce-ad80-ccadf8e82185\",\"os\":\"1\",\"crid\":\"1-139\",\"domain\":\"\",\"appbundle\":\"1252520771\",\"bidstmp\":\"1554007429257\",\"price\":\"0.544\",\"actualbid\":\"null\",\"ip\":\"13.251.89.213\",\"stmp\":\"2019-03-31 04:43:52\"}";

        int st = s.indexOf("{");

        System.out.println(s.substring(st, s.length()));
    }
}
